/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.postbar.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.ssl.SslSocketConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

/**
 *
 * @author wanglin
 */
public class SSLServerDemo {

    public static int port = 8121;//端口
    public static int parallelSize = 256;//并行度

    public static void main(String[] args) throws Exception {

        //server
        Server server = new Server();

        //ssl connector
        SslSocketConnector ssl_connector = new SslSocketConnector();
        ssl_connector.setPort(port);
        //set ssl key storage infomation
        SslContextFactory cf = ssl_connector.getSslContextFactory();
        /*
		 * 生成秘钥文件最简单的命令：
		 * keytool -keystore test.keystore -genkey -keyalg RSA
		 *
		 * 注意：
		 * keytool命令在jdk的bin目录下。
		 * 该命令生成的文件位置在 shell/命令行 的当前位置。
		 * 其中 test.keystore 是文件名，可以更改，或者移动文件。
		 *
		 * 下方代码里：
		 * path可以自己指定，可以把生成的秘钥文件移动到指定位置
		 * 两个password在生成过程中指定
         */
        cf.setKeyStorePath("src/test/resources/test.keystore");
        cf.setKeyStorePassword("123456");
        cf.setKeyManagerPassword("123456");
        //add connector to server
        server.addConnector(ssl_connector);

        //server configurations
        server.setThreadPool(new QueuedThreadPool(parallelSize));

        //context
//		ContextHandlerCollection contexts = new ContextHandlerCollection();
//		RootHandler rootHandler = RootHandler.getRootHandler(HandlerFactory.getHandlerFactory());
//		if (rootHandler == null) {
//			throw new Exception("initializing root handler is failed");
//		}
//		ContextHandler contextHandler = new ContextHandler("/hellossl");
//		contextHandler.setHandler(rootHandler);
//		contexts.addHandler(contextHandler);
//
//		HandlerFactory.getHandlerFactory().registerHandler("/hellossl", "/", HelloSSL.class);
        //server.setHandler(contexts);
        server.setHandler(new HelloHandler());
        server.start();
        server.join();
    }

    public static class HelloHandler extends AbstractHandler {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.setContentType("text/html;charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);
            baseRequest.setHandled(true);
            PrintWriter writer = response.getWriter();
            writer.println("Hello World !");
            BufferedReader reader = request.getReader();
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
            }
            System.out.println("schema: " + request.getScheme());
            writer.println("schema: " + request.getScheme());
            System.out.println("target: " + target);
            writer.println("target: " + target);;
            System.out.println("request method : " + request.getMethod());
            writer.println("request method : " + request.getMethod());
            String line;
            writer.println("request string :");
            System.out.println("request string :");
            while ((line = reader.readLine()) != null) {
                writer.println(line);
            }
            writer.flush();
        }
    }
}
